package com.lnu.information;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;



public class HttpUtil {

//    private static final String URL = "http://125.222.68.211:9235/api/join/company&company_listedcompany_toptencurrentshareholders";

//    private static final String URL = "http://localhost:8082/api/join/company&company_listedcompany_toptencurrentshareholders";

    //单表查询url
    private static final String URL = "http://192.144.230.69:8082/api/join/company&company_listedcompany_toptencurrentshareholders";

//    private static final String URL1 = "http://125.222.68.211:9235/api/company_listedcompany_detailinfor";

//      private static final String URL1 = "http://localhost:8082/api/company_listedcompany_detailinfor";

    //连表查询url
    private static final String URL1 = "http://192.144.230.69:8082/api/company_listedcompany_detailinfor";
    /***
     * post请求参数为json格式
     * @param url
     * @param json
     * @return
     */
    public static String doJsonPost(String url, String json) {
        String result = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            HttpPost httpPost = new HttpPost(url);
            StringEntity requestEntity = new StringEntity(json,"UTF-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);
            result = httpClient.execute(httpPost, responseHandler);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return result;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        //必填
        jsonObject.put("appKey","10001");
        //单表查询填
        jsonObject.put("companyName","指南针");
        //连表查询填
        jsonObject.put("companyId","CP0000000001");
        String result = doJsonPost(URL1, jsonObject.toJSONString());
        System.out.println(result);
    }
}